import apiClient from './api';
import { ModelsResponse } from '@/types/api';

export interface ModelsQueryParams {
  use_pagination?: boolean;
  page?: number;
  page_size?: number;
  search?: string;
  type?: string;
  status?: string;
}

/**
 * 获取模型列表
 */
export async function getModels(params?: ModelsQueryParams): Promise<ModelsResponse> {
  try {
    let url = '/models';
    
    if (params) {
      const searchParams = new URLSearchParams();
      
      if (params.use_pagination) searchParams.append('use_pagination', 'true');
      if (params.page) searchParams.append('page', params.page.toString());
      if (params.page_size) searchParams.append('page_size', params.page_size.toString());
      if (params.search) searchParams.append('search', params.search);
      if (params.type) searchParams.append('type', params.type);
      if (params.status) searchParams.append('status', params.status);
      
      const queryString = searchParams.toString();
      if (queryString) url += `?${queryString}`;
    }
    
    const response = await apiClient.get(url);
    return response as unknown as ModelsResponse;
  } catch (error) {
    console.error('获取模型列表失败:', error);
    throw error;
  }
}

/**
 * 导入模型
 */
export async function importModel(file: File, metadata?: any): Promise<any> {
  try {
    const formData = new FormData();
    formData.append('file', file);
    if (metadata) {
      formData.append('metadata', JSON.stringify(metadata));
    }
    
    const response = await apiClient.post('/models/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    return response;
  } catch (error) {
    console.error('导入模型失败:', error);
    throw error;
  }
}

export const modelsAPI = {
  getModels,
  importModel
}; 